{/* Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License. */}

import colorTypes from 'docs:@react-types/color/src/index.d.ts';
import docs from 'docs:@react-spectrum/color';
import {HeaderInfo, PropTable, TypeLink, PageDescription} from '@react-spectrum/docs';
import {Layout} from '@react-spectrum/docs';
import packageData from '@react-spectrum/color/package.json';
import statelyDocs from 'docs:@react-stately/color';

export default Layout;

```jsx import
import {ColorSwatchPicker, ColorSwatch} from '@react-spectrum/color';
```

---
category: Color
keywords: [color swatch]
---

# ColorSwatchPicker

<PageDescription>{docs.exports.ColorSwatchPicker.description}</PageDescription>

<HeaderInfo
  packageData={packageData}
  componentNames={['ColorSwatchPicker']}
  sourceData={[
    {type: 'Spectrum', url: 'https://spectrum.adobe.com/page/swatch-group/'}
  ]}
  since="3.35.0" />

## Example

```tsx example
<ColorSwatchPicker>
  <ColorSwatch color="#A00" />
  <ColorSwatch color="#f80" />
  <ColorSwatch color="#080" />
  <ColorSwatch color="#08f" />
  <ColorSwatch color="#088" />
  <ColorSwatch color="#008" />
</ColorSwatchPicker>
```

## Value

ColorSwatchPicker accepts either an uncontrolled default value or a controlled value, provided using the `defaultValue` or `value` props respectively. The value provided to either of these props should be a color string or <TypeLink links={colorTypes.links} type={colorTypes.exports.Color} /> object. The value is matched against the color of each ColorSwatch, including equivalent colors in different color spaces.

In the example below, the <TypeLink links={statelyDocs.links} type={statelyDocs.exports.parseColor} /> function is used to parse the initial color from a HSL string so that `value`'s type remains consistent.

```tsx example
import {parseColor} from '@react-spectrum/color';

function Example() {
  let [color, setColor] = React.useState(parseColor('hsl(0, 100%, 33.33%)'));

  return (
    <ColorSwatchPicker value={color} onChange={setColor}>
      <ColorSwatch color="#A00" />
      <ColorSwatch color="#f80" />
      <ColorSwatch color="#080" />
    </ColorSwatchPicker>
  );
}
```

**Note**: ColorSwatches rendered inside a ColorSwatchPicker must have unique colors, even between different color spaces. For example, the values `#f00`, `hsl(0, 100%, 50%)`, and `hsb(0, 100%, 100%)` are all equivalent and considered duplicates. This is required so that selection behavior works properly.

## Labeling

By default, `ColorSwatchPicker` has an `aria-label` with the localized string "Color swatches". This can be overridden with a more specific accessibility label using the `aria-label` or `aria-labelledby` props. All labels should be localized.

```tsx example
<ColorSwatchPicker aria-label="Fill color">
  <ColorSwatch color="#A00" />
  <ColorSwatch color="#f80" />
  <ColorSwatch color="#080" />
</ColorSwatchPicker>
```

## Events

ColorSwatchPicker accepts an `onChange` prop which is triggered whenever the value is edited by the user. It receives a <TypeLink links={colorTypes.links} type={colorTypes.exports.Color} /> object as a parameter.

The example below uses `onChange` to update a separate element with the color value as a string.

```tsx example
import {parseColor} from '@react-spectrum/color';

function Example() {
  let [value, setValue] = React.useState(parseColor('#A00'));

  return (
    <div>
      <ColorSwatchPicker value={value} onChange={setValue}>
        <ColorSwatch color="#A00" />
        <ColorSwatch color="#f80" />
        <ColorSwatch color="#080" />
        <ColorSwatch color="#08f" />
        <ColorSwatch color="#088" />
        <ColorSwatch color="#008" />
      </ColorSwatchPicker>
      <p>Selected color: {value.toString('rgb')}</p>
    </div>
  );
}
```

## Props

<PropTable component={docs.exports.ColorSwatchPicker} links={docs.links} />

## Visual options

### Size

[View guidelines](https://spectrum.adobe.com/page/swatch-group/#Size)

```tsx example
<ColorSwatchPicker size="XS">
  <ColorSwatch color="#A00" />
  <ColorSwatch color="#f80" />
  <ColorSwatch color="#080" />
  <ColorSwatch color="#08f" />
</ColorSwatchPicker>
```

### Density

[View guidelines](https://spectrum.adobe.com/page/swatch-group/#Density)

```tsx example
<ColorSwatchPicker density="compact">
  <ColorSwatch color="#A00" />
  <ColorSwatch color="#f80" />
  <ColorSwatch color="#080" />
  <ColorSwatch color="#08f" />
</ColorSwatchPicker>
```

### Rounding

[View guidelines](https://spectrum.adobe.com/page/swatch-group/#Corner-rounding-in-swatch-groups)

Only use rounded corners if the ColorSwatchPicker is displayed on a single row.

```tsx example
<ColorSwatchPicker rounding="full">
  <ColorSwatch color="#A00" />
  <ColorSwatch color="#f80" />
  <ColorSwatch color="#080" />
  <ColorSwatch color="#08f" />
</ColorSwatchPicker>
```
